package us.kg.kotoriforest.senior.dao.impl;

import us.kg.kotoriforest.senior.dao.BaseDAO;
import us.kg.kotoriforest.senior.dao.EmployeeDAO;
import us.kg.kotoriforest.senior.pojo.Employee;

import java.sql.SQLException;
import java.util.List;

/**
 * ClassName: us.kg.kotoriforest.senior.dao.impl.EmployeeDAOImpl </br>
 * Description: 操作 t_emp 表的 DAO 实现类
 *
 * @author Kotori rotroutine@163.com
 * @since 2024/11/29
 */
public class EmployeeDAOImpl extends BaseDAO implements EmployeeDAO {
    @Override
    public List<Employee> selectAll() {
        String SQL = "SELECT emp_id AS empId, emp_name AS empName, emp_salary AS empSalary, emp_age AS empAge FROM t_emp";
        try {
            return executeQuery(Employee.class, SQL);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Employee selectByEmpId(int empId) {
        String SQL = "SELECT emp_id AS empId, emp_name AS empName, emp_salary AS empSalary, emp_age AS empAge FROM t_emp WHERE emp_id = ?";
        try {
            return executeQueryGetFirst(Employee.class, SQL, empId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(Employee employee) {
        String SQL = "INSERT INTO t_emp(emp_name, emp_salary, emp_age) VALUES(?, ?, ?)";
        try {
            return executeUpdate(SQL, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpSalary());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(Employee employee) {
        String SQL = "UPDATE t_emp SET emp_name = ?, emp_salary = ?, emp_age = ? WHERE emp_id = ?";
        try {
            return executeUpdate(SQL, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpAge(), employee.getEmpId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delete(int empId) {
        String SQL = "DELETE FROM t_emp WHERE emp_id = ?";
        try {
            return executeUpdate(SQL, empId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
